package com.hexiang.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hexiang.domain.User;
import com.hexiang.service.UserService;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Component
public class UserHandler {

    @Autowired
    private UserService userService;

    public Mono<User> findByName(String name) {
        return Mono.justOrEmpty(userService.findByName(name));
    }

    public Mono<User> saveByUser(User user) {
        return Mono.create(userMonoSink -> userMonoSink.success(userService.saveByUser(user)));
    }

    public Mono<Integer> save(String name, Integer age) {
        return Mono.create(userMonoSink -> userMonoSink.success(userService.save(name, age)));
    }

    public Mono<Integer> update(User user) {
        return Mono.create(userMonoSink -> userMonoSink.success(userService.update(user)));
    }

    public Mono<Integer> delete(Long id) {
        return Mono.create(userMonoSink -> userMonoSink.success(userService.delete(id)));
    }

    public Flux<User> findAll() {
        return Flux.fromIterable(userService.findAll());

    }
}